<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>使用字典动态构造where子句 (web.py)</title> 
  <link rel="stylesheet" type="text/css" href="/static/webpy-new.css"/> 
  <link rel="SHORTCUT ICON" href="/static/favicon.ico"/> 
</head><body> 
 
        <div id="header"><table class="header"><tr>
                <td class="logo"><a href="/" title="Home"><img src="/static/webpy.gif" alt="" /></a></td> 
<td class="blurb">&ldquo;Think about the ideal way to write a web app.<br />Write the code to make it happen.&rdquo; [<a href="/philosophy">more...</a>]</td> </tr>
</table>
        </div> 
        <div id="location"> 
                <a href="/">home</a> &gt; 使用字典动态构造where子句
        </div> 
<div id="container"> 
<div id="sidebar">
<strong>get started:</strong>
<ul class="body">
  <li><a href="/install">install</a></li>
  <li><a href="/docs/0.3/tutorial">tutorial</a></li>
</ul>
<strong>learn more:</strong>
<ul class="body">
<li><a href="/docs/0.3/api">api reference</a></li>
<li><a href="/cookbook/">cookbook</a></li>
<li><a href="/src/">code examples</a></li>
</ul>

<strong>dive in:</strong>
<ul class="body"> 
<li><a href="https://github.com/webpy/webpy">source code</a></li>
<li><a href="http://groups.google.com/group/webpy/">mailing list</a></li>
<li><a href="https://github.com/webpy/webpy/issues">issue tracker</a></li>
</ul>
</div>

<div id="main"> 
<div id="content" class="body"> 
<h1>使用字典动态构造where子句</h1>

<h2>问题</h2>

<p>你希望创建一个字典来构造动态的where子句并且希望能够在查询语句中使用。</p>

<h2>解决</h2>

<pre><code>&gt;&gt;&gt; import web
&gt;&gt;&gt; db = web.database(dbn='postgres', db='mydb', user='postgres')
&gt;&gt;&gt; where_dict = {'col1': 1, col2: 'sometext'}
&gt;&gt;&gt; db.delete('mytable', where=web.db.sqlwhere(where_dict), _test=True)
&lt;sql: "DELETE FROM mytable WHERE col1 = 1 AND col2 = 'sometext'"&gt;
</code></pre>

<h2>解释</h2>

<p><code>web.db.sqlwhere</code> takes a Python dictionary as an argument and converts it into a string useful for where clause in different queries. You can also use an optional <code>grouping</code> argument to define the exact gouping of the individual keys. For instance:</p>

<p><code>web.db.sqlwhere</code>将Python的字典作为参数并且转换为适用于不同的查询语句的where子句的string类型数据。你也可以使用<code>grouping</code>参数来定义链接字典中的key的链接字符。例子如下。</p>

<pre><code>&gt;&gt;&gt; import web
&gt;&gt;&gt; web.db.sqlwhere({'a': 1, 'b': 2}, grouping=' OR ')
'a = 1 OR b = 2'
</code></pre>

<p><code>grouping</code> 的默认值为 <code>' AND '</code>.</p>

<div style="clear: both;"></div> 
 
</div> 
 
</div> 
</div>
        <div id="footer"> 
    <p>started by <a style="color: gray;" href="http://www.aaronsw.com/">Aaron Swartz</a> (<a style="color: black;" href="mailto:webpy@aaronsw.com">webpy@aaronsw.com</a>)</p>

<p><a href="https://github.com/webpy/webpy.github.com/blob/master/cookbook/where_dict.zh-cn.md">fork this page on GitHub</a></p>
</div> 
 
</body> 
</html> 
